package com.example.aboutreview;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

/**
 * Since enum types ensure that only one instance of the constants exist in the JVM, we can safely use “==” operator to
 * compare two variables as seen in the example above; moreover the “==” operator provides compile-time and run-time safety.
 *
 * n addition to that, since the enum class implements the Serializable interface under the hood, the class is guaranteed to be
 * a singleton by the JVM, which unlike the conventional implementation where we have to ensure that no new instances are created
 * during deserialization.
 */
@Data
public class EnumUse {


    public PizzaDeliveryStrategy strategy;

    @Test
    public void 单利测试(){
        PizzaDeliveryStrategy deliveryStrategy = SingletonIns.INSTANCE.getDeliveryStrategy();
        PizzaDeliveryStrategy deliveryStrategy1 = SingletonIns.INSTANCE.getDeliveryStrategy();
        System.err.println(deliveryStrategy==deliveryStrategy1);
        EnumUse enumUse = new EnumUse();
        PizzaDeliveryStrategy.EXPRESS.deliver(enumUse);
        System.err.println(enumUse.strategy);
    }

}

@NoArgsConstructor
enum SingletonIns {
    INSTANCE;

    public static SingletonIns getInstance() {
        return INSTANCE;
    }

    private PizzaDeliveryStrategy deliveryStrategy = PizzaDeliveryStrategy.NORMAL;

    public PizzaDeliveryStrategy getDeliveryStrategy() {
        return deliveryStrategy;
    }

}

/**
 * 策略模式的实现
 */
enum PizzaDeliveryStrategy {
    NORMAL{
        @Override
        void deliver(EnumUse enumUse) {
            System.err.println("普通方式");
            enumUse.setStrategy(this);
        }
    },
    EXPRESS{
        @Override
        void deliver(EnumUse enumUse) {
            System.err.println("特快方式");
            enumUse.setStrategy(this);
        }
    };



    abstract void deliver(EnumUse enumUse);
}